package DAL;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import entities.ConnectionManager;
import entities.VotingCandidates;

public class VotingCandidatesDA {
	public VotingCandidates[] selectVotingCandidates(VotingCandidates user)
	{
		boolean whereClause = false;
		String query = "SELECT * from VotingCandidates  " ;
		if(user.getAdminID() != -1)
		{
			query += "WHERE AdminID = " + user.getAdminID();
			whereClause = true;
		}
				
		if(user.getCandidateID() != -1) 
		{
			if(whereClause)
				query += " AND CandidateID = " + user.getCandidateID() + "";
			else
				query += " WHERE CandidateID = " + user.getCandidateID() + "";
			whereClause = true;
		}
		if(user.getVotingID() != -1) 
		{
			if(whereClause)
				query += " AND VotingID = " + user.getVotingID() + "";
			else
				query += " WHERE VotingID = " + user.getVotingID() + "";
			whereClause = true;
		}
		if(user.getUserIDCandidate()!= -1) 
		{
			if(whereClause)
				query += " AND UserIDCandidate = " + user.getUserIDCandidate() + "";
			else
				query += " WHERE UserIDCandidate = " + user.getUserIDCandidate() + "";
			whereClause = true;
		}
		ConnectionManager con = new ConnectionManager();
		
		ResultSet rs = con.executeSelectQuery(query);
		
		ArrayList<VotingCandidates> users = new ArrayList<VotingCandidates>();

		
		try {
			while(rs.next())
			{
				VotingCandidates tempUser= new VotingCandidates();
				tempUser.setAdminID(rs.getInt("adminID"));
				tempUser.setVotingID(rs.getInt("VotingID"));
				tempUser.setUserIDCandidate(rs.getInt("UserIDCandidate"));
				tempUser.setCandidateID(rs.getInt("candidateID"));
				users.add(tempUser);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		VotingCandidates [] temp = new VotingCandidates [0];
		return users.toArray(temp);

	}


	public boolean updateVotingCandidates(VotingCandidates user)
	{
		boolean setClause = false;
		String query = "UPDATE VotingCandidates SET " ;
		if(user.getAdminID()!= -1) 
		{
			query += " AdminID = " + user.getAdminID() + "  ";
			setClause = true;
		}
		if(user.getCandidateID()!= -1) 
		{
			if(setClause)
				query += " , CandidateID = " + user.getCandidateID() + "";
			else
				query += " CandidateID = " + user.getCandidateID() + "";
			setClause = true;
		}
		if(user.getVotingID()!= -1) 
		{
			if(setClause)
				query += " , VotingID = " + user.getVotingID() + "";
			else
				query += " VotingID = " + user.getVotingID() + "";
			setClause = true;
		}
		if(user.getUserIDCandidate()!= -1) 
		{
			if(setClause)
				query += " , UserIDCandidate = " + user.getUserIDCandidate() + "";
			else
				query += " UserIDCandidate = " + user.getUserIDCandidate() + "";
			setClause = true;
		}
		
		
		
		query += " WHERE CandidateID = "+ user.getCandidateID() 
		+ " AND AdminID = " + user.getAdminID()
		+ " AND VotingID = " + user.getVotingID()
		+ " AND UserIDCandidate = " + user.getUserIDCandidate();
		
		ConnectionManager con = new ConnectionManager();
		
		int x = con.executeUpdateQuery(query);
		
		return (x==1);
	}
	
	public boolean insertVotingCandidates(VotingCandidates user)
	{
		String query = "INSERT INTO VotingCandidates (CandidateID , AdminID , UserIDCandidate,VotingID ) VALUES " +
				"(" +user.getCandidateID() + " , " + user.getAdminID() + " , " + user.getUserIDCandidate() + 
				" , '" + user.getVotingID() +  "')";
		
		ConnectionManager con = new ConnectionManager();

		int x = con.executeUpdateQuery(query);
		
		return x==1;
	}
	
	public int deleteVotingCandidates(VotingCandidates user)
	{
		boolean whereClause = false;
		String query = "DELETE FROM VotingCandidates ";
		if(user.getAdminID() != -1)
		{
			query += "WHERE AdminID = " + user.getAdminID();
			whereClause = true;
		}
				
		if(user.getCandidateID() != -1) 
		{
			if(whereClause)
				query += " AND CandidateID = " + user.getCandidateID() + "";
			else
				query += " WHERE CandidateID = " + user.getCandidateID() + "";
			whereClause = true;
		}
		if(user.getVotingID() != -1) 
		{
			if(whereClause)
				query += " AND VotingID = " + user.getVotingID() + "";
			else
				query += " WHERE VotingID = " + user.getVotingID() + "";
			whereClause = true;
		}
		if(user.getUserIDCandidate()!= -1) 
		{
			if(whereClause)
				query += " AND UserIDCandidate = '" + user.getUserIDCandidate() + "'";
			else
				query += " WHERE UserIDCandidate = '" + user.getUserIDCandidate() + "'";
			whereClause = true;
		}
		
		ConnectionManager con = new ConnectionManager();
		
		int x = con.executeUpdateQuery(query);
		
		return x;
	}
	
	
	public static void main(String[] args) {
		VotingCandidates u = new VotingCandidates();

		u.setAdminID(1);
		u.setVotingID(2);
		u.setCandidateID(5);
		u.setUserIDCandidate(6);
		
		VotingCandidatesDA da = new VotingCandidatesDA();
		System.out.println(da.insertVotingCandidates(u));
	}

}
